package com.yyzw.activiti.modeler.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyzw.activiti.modeler.dao.FlowMapper;
import com.yyzw.activiti.modeler.model.*;
import com.yyzw.activiti.modeler.service.FlowService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricVariableUpdate;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class FlowServiceImpl implements FlowService {
    @Autowired
    FlowMapper flowMapper;

    @Autowired
    HistoryService historyService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;
    @Override
    public void getHistoryTaskByProcId(String processInstanceId) {
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().
                processInstanceId(processInstanceId).orderByProcessInstanceStartTime().desc().list();

    }

    @Override
    public DataGridRespVo getFlowList(int userId, int pageNum, int rows) {
        Example example =new Example(SysProjectApply.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        List<SysProjectApply> applies = this.flowMapper.selectByExample(example);
        DataGridRespVo dataGridRespVo =new DataGridRespVo();
        PageHelper.startPage(pageNum, rows);
        PageInfo<SysProjectApply> pageInfo = new PageInfo<>(applies);
        dataGridRespVo.setRows(applies);
        dataGridRespVo.setTotal(pageInfo.getTotal());
        return dataGridRespVo;
    }

    @Override
    public void addFlow(SysProjectApply sysProjectApply) {
      this.flowMapper.insert(sysProjectApply);
    }

    @Override
    public ResponseVo startFlowAndUpdate(SysProjectApply sysProjectApply) {

        Map<String,Object> map =new HashMap<>();
        map.put("applyData",sysProjectApply);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("mydemo",map);
        sysProjectApply.setProcessInstanceId(processInstance.getId());
        sysProjectApply.setStatus("0");//0表示审批中。。。
        sysProjectApply.setCreateTime(new Date());
        this.flowMapper.updateByPrimaryKey(sysProjectApply);

        return new ResponseVo(true,"发送成功");
    }

    @Override
    public List<TaskHandleVo> getFlowProcess(String processInstanceId) {
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        List<TaskHandleVo>  handleInfos = null;
        if(instance!=null) {
            Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
            Object variable = taskService.getVariable(task.getId(), "handleOptionin");
            if(variable!=null){
                handleInfos =( List<TaskHandleVo>) variable;
            }
        }else{
            //查询历史流转信息，当任务结束时
            handleInfos = new ArrayList<>();
            List<HistoricDetail> list = historyService.createHistoricDetailQuery().processInstanceId(processInstanceId).list();
            HistoricVariableUpdate variableUpdate =null;
            for(HistoricDetail historicDetail:list){
                variableUpdate =(HistoricVariableUpdate)historicDetail;
                if("handleOptionin".equals(variableUpdate.getVariableName())){
                    handleInfos.clear();;
                    handleInfos.addAll((List<TaskHandleVo>)variableUpdate.getValue());
                }
            }
        }
        return handleInfos;
    }
}
